package alibaba.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class CommonUtils {

	public static final char UNIX_SEPARATOR = '/';
	
	public static final char WINDOWS_SEPARATOR = '\\';
	
    public static final char LINE_BREAK = '\n';
    
    public static boolean readInputStreamIntoOutputStream(InputStream is, OutputStream os) {
        try {
            final int chunkSize = 2048;
            byte[] buf = new byte[chunkSize];
            int count;
            while ((count = is.read(buf)) != -1) {
                os.write(buf, 0, count);
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    
    public static boolean readReaderIntoWriter(Reader reader, Writer writer) {
        try {
            final int chunkSize = 2048;
            char[] buf = new char[chunkSize];
            int count;
            while ((count = reader.read(buf)) != -1) {
                writer.write(buf, 0, count);
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    
    public static String makeEndWithUnixSeparator(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
    	return str + ((str.charAt(str.length()-1) == CommonUtils.UNIX_SEPARATOR) ?
    			"" : String.valueOf(CommonUtils.UNIX_SEPARATOR));			
    }

    public static String makeNotEndWithUnixSeparator(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        return (str.charAt(str.length()-1) == CommonUtils.UNIX_SEPARATOR) ? 
               str.substring(0, str.length()-1) : str;
    }
    
    public static String makeStartWithUnixSeparator(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        return ((str.charAt(0) == CommonUtils.UNIX_SEPARATOR) ?
                "" : String.valueOf(CommonUtils.UNIX_SEPARATOR)) + str;           
    }
    
}
